package com.doyens.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doyens.common.R;
import com.doyens.dto.ClassDto;
import com.doyens.entity.*;
import com.doyens.entity.ClassRoom;
import com.doyens.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping("/class")
@Api(tags = "班级管理接口")
public class ClassController {
    @Autowired
    private ClassService classService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private ClassTeacherService classTeacherService;

    @Autowired
    private ProfessionService professionService;

    @Autowired
    private TeachingCycleService teachingCycleService;

    @GetMapping("/list")
    @ApiOperation(value = "班级下拉框查询班级列表方法")
    public R<List<ClassRoom>> nameList(){
        List<ClassRoom> list = classService.list();
        return R.success(list);
    }


    @GetMapping("/page")
    @ApiOperation(value = "班级信息分页列表方法")
    public R<Page> page(int page, int pageSize) {
        //构造分页条件
        Page<ClassRoom> pageInfo = new Page<>(page, pageSize);
        Page<ClassDto> classDtoPage = new Page<>();

        //条件构造器
        LambdaQueryWrapper<ClassRoom> queryWrapper = new LambdaQueryWrapper<>();

        //添加排序条件
        queryWrapper.orderByDesc(ClassRoom::getCreateTime);
        //执行分页
        classService.page(pageInfo, queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo, classDtoPage, "records");
        List<ClassRoom> records = pageInfo.getRecords();
        List<ClassDto> list = records.stream().map((item) -> {

            ClassDto classDto = new ClassDto();
            BeanUtils.copyProperties(item, classDto);

            //获取班级id，查询班级对应学生数量
            Integer classId = item.getClassId();
            LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Student::getClassId,classId);
            int count = studentService.count(wrapper);
            classDto.setStudentCount(count);
            //获取教学周期id，查询对应周期
            Integer teachingCycleId = item.getTeachingCycleId();
            TeachingCycle teachingCycleName = teachingCycleService.getById(teachingCycleId);
            //获取讲师id，查询班级对应讲师
            Integer teacherId = item.getTeacherId();
            Teacher teacher = teacherService.getById(teacherId);
            //获取班主任id，查询对应班主任姓名
            Integer classTeacherId = item.getClassTeacherId();
            ClassTeacher classTeacher = classTeacherService.getById(classTeacherId);
            //获取专业id，查询班级对应的学习专业
            Integer professionId = item.getProfessionId();
            Profession profession = professionService.getById(professionId);

            if (teachingCycleName != null) {
                String teachingCycle = teachingCycleName.getTeachingCycle();
                classDto.setTeachingCycle(teachingCycle);
            }
            if (teacher != null) {
                String teacherName = teacher.getTeacherName();
                classDto.setTeacherName(teacherName);
            }
            if (classTeacher != null) {
                String classteacherName = classTeacher.getClassteacherName();
                classDto.setClassteacherName(classteacherName);
            }
            if (profession != null) {
                String professionalName = profession.getProfessionalName();
                classDto.setProfessionalName(professionalName);
            }
            return classDto;
        }).collect(Collectors.toList());
        classDtoPage.setRecords(list);
        return R.success(classDtoPage);
    }

    @DeleteMapping
    @ApiOperation(value = "删除班级方法")
    public R<String> delete(Integer id) {
        boolean b = classService.removeById(id);
        if (b){
            return R.success("删除班级成功");
        }
        return R.error("删除班级失败");
    }

    @PostMapping
    @ApiOperation(value = "添加班级方法")
    public R<String> save(ClassRoom classRoom) {
        boolean save = classService.save(classRoom);
        if (save){
            return R.success("新增班级成功");
        }
        return R.error("新增班级失败");
    }

    @GetMapping("{id}")
    @ApiOperation(value = "根据id查询班级信息")
    public R<ClassDto> getById(@PathVariable String id) {
        ClassRoom classRoom = classService.getById(id);
        ClassDto classDto = new ClassDto();
        BeanUtils.copyProperties(classRoom,classDto);
        //根据班级id查询班级学生数量
        Integer classId = classRoom.getClassId();
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Student::getClassId,classId);
        int count = studentService.count(wrapper);
        classDto.setStudentCount(count);
        //查询班级对应讲师
        Integer teacherId = classRoom.getTeacherId();
        Teacher teacher = teacherService.getById(teacherId);
        if (teacher!=null){
            String teacherName = teacher.getTeacherName();
            classDto.setTeacherName(teacherName);
        }
        //查询班级对应班主任
        Integer classTeacherId = classRoom.getClassTeacherId();
        ClassTeacher classTeacher = classTeacherService.getById(classTeacherId);
        if (classTeacher!=null){
            String classteacherName = classTeacher.getClassteacherName();
            classDto.setClassteacherName(classteacherName);
        }
        //查询班级对应教学周期
        Integer teachingCycleId = classRoom.getTeachingCycleId();
        TeachingCycle teachingCycle = teachingCycleService.getById(teachingCycleId);
        if (teachingCycle!=null){
            String teachingCycle1 = teachingCycle.getTeachingCycle();
            classDto.setTeachingCycle(teachingCycle1);
        }
        //查询班级对应专业
        Integer professionId = classRoom.getProfessionId();
        Profession profession = professionService.getById(professionId);
        if (profession!=null){
            String professionalName = profession.getProfessionalName();
            classDto.setProfessionalName(professionalName);
        }
        if (classRoom != null) {
            return R.success(classDto);
        }
        return R.error("没有查询到班级信息");
    }

    @PutMapping
    @ApiOperation(value = "修改班级信息")
    public R<String> update(ClassRoom classRoom) {
        boolean b = classService.updateById(classRoom);
        if (b){
            return R.success("修改班级信息成功");
        }
        return R.error("更新班级信息失败");
    }
}
